package com.sansecy.video.player;

import android.content.Context;
import android.content.SharedPreferences;

public class PlayerUtil {
    public static final String VIDEO_PLAY_PROGRESS = "video_play_progress";


    public static void seekTo(Context context, IPlayer player, String uri) {
        long savedPlayPosition = PlayerUtil.getSavedPlayPosition(context, uri);
        long savedPlayDuration = PlayerUtil.getSavedPlayDuration(context, uri);
        if (savedPlayPosition > 0 && savedPlayDuration - savedPlayPosition > 2000) {
            player.seekTo(savedPlayPosition);
        }
    }

    public static long getSavedPlayPosition(Context context, String url) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        return videoPlayProgress.getLong(url, 0);
    }

    public static void savePlayPosition(Context context, String url, long position) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        videoPlayProgress.edit().putLong(url, position).apply();
    }

    public static long getSavedPlayDuration(Context context, String url) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        return videoPlayProgress.getLong(url + "_duration", 0);
    }

    public static void savePlayDuration(Context context, String url, long duration) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        videoPlayProgress.edit().putLong(url + "_duration", duration).apply();
    }

    public static String getSavedPlaySubtitleUri(Context context, String url) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        return videoPlayProgress.getString(url + "_subtitle", "");
    }

    public static void savePlaySubtitle(Context context, String url, String subtitleUri) {
        SharedPreferences videoPlayProgress = context.getSharedPreferences(VIDEO_PLAY_PROGRESS, Context.MODE_PRIVATE);
        videoPlayProgress.edit().putString(url + "_subtitle", subtitleUri).apply();
    }
}